package com.homer.gps.track.helper;

import com.homer.gps.track.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

public class AnimatedLinearLayout extends LinearLayout {

	private Animation animHide;
	private Animation animShow;

	public AnimatedLinearLayout(Context context) {
		super(context);
		init();
	}

	public AnimatedLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public AnimatedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		animHide = AnimationUtils.loadAnimation(getContext(), R.anim.slide_out_down);
		animHide.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				setVisibility(GONE);
			}
		});

		animShow = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_up);
		animShow.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				setVisibility(VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
			}
		});
	}

	public void hide() {
		if (getVisibility() == GONE) {
			return;
		}
		startAnimation(animHide);
	}

	public void show() {
		if (getVisibility() == VISIBLE) {
			return;
		}
		startAnimation(animShow);
	}

	public boolean isVisible() {
		return getVisibility() == VISIBLE;
	}

}
